package com.doublefish.doublefishrobot.ui.home.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.utils.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 作者:dugaofeng on 2024/10/18 16:35
 */
public class ModeSettingItemValueView extends LinearLayout {

    private Context mContext;

    private TextView valueTypeTv;

    private TextView subtractTv;
    private EditText editText;
    private TextView addTv;

    private OnModeSettingItemValueViewListner onModeSettingItemValueViewListner;

    //0.固定值  1.最大值  2.最小值
    private int type;
    private int maxValue;
    private int minValue;
    private int currentValue;

    public ModeSettingItemValueView(@NonNull Context context) {
        this(context, null);
    }

    public ModeSettingItemValueView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ModeSettingItemValueView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
        initListner();
    }

    private void initView() {

        inflate(getContext(), R.layout.view_mode_setting_item_value, this);

        valueTypeTv = findViewById(R.id.tv_value_type);
        editText = findViewById(R.id.edittext);

        subtractTv = findViewById(R.id.tv_subtract);
        addTv = findViewById(R.id.tv_add);
    }

    private void initListner() {
        subtractTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    currentValue = Integer.parseInt(editText.getText().toString());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                if (currentValue > minValue) {
                    currentValue--;

                    String text = "" + currentValue;
                    editText.setText(text);
                    editText.setSelection(text.length());
                    shouldCallbackValueChanged();
                }
            }
        });

        addTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    currentValue = Integer.parseInt(editText.getText().toString());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                if (currentValue < maxValue) {
                    currentValue++;

                    String text = "" + currentValue;
                    editText.setText(text);
                    editText.setSelection(text.length());
                    shouldCallbackValueChanged();
                }
            }
        });

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    // EditText失去焦点，这里处理你的逻辑
                    String inputText = editText.getText().toString();
                    onEditTextDidEndEditing(inputText);
                }
            }
        });
        //键盘回收的监听
        EventBus.getDefault().register(this);
    }

    public void setOnModeSettingItemValueViewListner(OnModeSettingItemValueViewListner onModeSettingItemValueViewListner) {
        this.onModeSettingItemValueViewListner = onModeSettingItemValueViewListner;
    }

    public void setType(int type) {
        this.type = type;
        if (type == 0) {
            valueTypeTv.setText(R.string.fixed_value);
        } else if (type == 1) {
            valueTypeTv.setText(R.string.maximum_value);
        } else if (type == 2) {
            valueTypeTv.setText(R.string.minimum_value);
        }
    }

    public void setValue(int maxValue, int minValue , int currentValue) {
        this.maxValue = maxValue;
        this.minValue = minValue;
        this.currentValue = currentValue;

        editText.setText("" + currentValue);
    }

    public void updateMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    public void updateMinValue(int minValue) {
        this.minValue = minValue;
    }

    public void clearFocus() {
        if (editText.isFocused()) {
            editText.clearFocus();
        }
    }

    private void onEditTextDidEndEditing(String text) {
        try {
            int value = Integer.parseInt(text.trim());

            if (value > maxValue) {
                value = maxValue;
                editText.setText("" + maxValue);
            }
            if (value < minValue) {
                value = minValue;
                editText.setText("" + minValue);
            }
            currentValue = value;
            shouldCallbackValueChanged();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    private void shouldCallbackValueChanged() {
        if (onModeSettingItemValueViewListner != null) {
            onModeSettingItemValueViewListner.onItemValueChanged(type, currentValue);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        EventBus.getDefault().unregister(this);
        super.onDetachedFromWindow();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MessageEvent event){
        if (event.getTag().equals(MessageEvent.KEYBOARD_HIDE)) {
            if (editText.isFocused()) {
                onEditTextDidEndEditing(editText.getText().toString());
            }
        }
    }

    public interface OnModeSettingItemValueViewListner {
        void onItemValueChanged(int type, int currentValue);
    }
}